home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / dpkg-maintscript-helper < prev    next >
Encoding:
Text File  |  2012-09-19  |  8.1 KB  |  325 lines

  1. #!/bin/sh
  2. #
  3. # Copyright ┬⌐ 2010 Rapha├½l Hertzog <hertzog@debian.org>
  4. # Copyright ┬⌐ 2008 Joey Hess <joeyh@debian.org>
  5. # Copyright ┬⌐ 2007 Guillem Jover (modifications on wiki.debian.org)
  6. # Copyright ┬⌐ 2005 Scott James Remnant (original implementation on www.dpkg.org)
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21. # The conffile related functions are inspired by
  22. # http://wiki.debian.org/DpkgConffileHandling
  23.  
  24. # This script is documented in dpkg-maintscript-helper(1)
  25.  
  26. ##
  27. ## Functions to remove an obsolete conffile during upgrade
  28. ##
  29. rm_conffile() {
  30.     local CONFFILE="$1"
  31.     local LASTVERSION="$2"
  32.     local PACKAGE="$3"
  33.     if [ "$LASTVERSION" = "--" ]; then
  34.         LASTVERSION=""
  35.         PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE"
  36.     fi
  37.     if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  38.         PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE"
  39.     fi
  40.     # Skip remaining parameters up to --
  41.     while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  42.     [ $# -gt 0 ] || badusage
  43.     shift
  44.  
  45.     [ -n "$PACKAGE" ] || error "couldn't identify the package"
  46.     [ -n "$1" ] || error "maintainer script parameters are missing"
  47.     [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  48.         error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  49.  
  50.     debug "Executing $0 rm_conffile in $DPKG_MAINTSCRIPT_NAME "\
  51.           "of $DPKG_MAINTSCRIPT_PACKAGE"
  52.     debug "CONFFILE=$CONFFILE PACKAGE=$PACKAGE "\
  53.           "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  54.     case "$DPKG_MAINTSCRIPT_NAME" in
  55.     preinst)
  56.         if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
  57.            dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  58.             prepare_rm_conffile "$CONFFILE" "$PACKAGE"
  59.         fi
  60.         ;;
  61.     postinst)
  62.         if [ "$1" = "configure" ] && [ -n "$2" ] &&
  63.            dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  64.             finish_rm_conffile $CONFFILE
  65.         fi
  66.         ;;
  67.     postrm)
  68.         if [ "$1" = "purge" ]; then
  69.             rm -f "$CONFFILE.dpkg-bak" "$CONFFILE.dpkg-remove" \
  70.                   "$CONFFILE.dpkg-backup"
  71.         fi
  72.         if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  73.            [ -n "$2" ] &&
  74.            dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  75.             abort_rm_conffile "$CONFFILE"
  76.         fi
  77.         ;;
  78.     *)
  79.         debug "$0 rm_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  80.         ;;
  81.     esac
  82. }
  83.  
  84. prepare_rm_conffile() {
  85.     local CONFFILE="$1"
  86.     local PACKAGE="$2"
  87.  
  88.     [ -e "$CONFFILE" ] || return 0
  89.  
  90.     local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  91.     local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  92.         sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  93.     if [ "$md5sum" != "$old_md5sum" ]; then
  94.         echo "Obsolete conffile $CONFFILE has been modified by you."
  95.         echo "Saving as $CONFFILE.dpkg-bak ..."
  96.         mv -f "$CONFFILE" "$CONFFILE.dpkg-backup"
  97.     else
  98.         echo "Moving obsolete conffile $CONFFILE out of the way..."
  99.         mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  100.     fi
  101. }
  102.  
  103. finish_rm_conffile() {
  104.     local CONFFILE="$1"
  105.  
  106.     if [ -e "$CONFFILE.dpkg-backup" ]; then
  107.         mv -f "$CONFFILE.dpkg-backup" "$CONFFILE.dpkg-bak"
  108.     fi
  109.     if [ -e "$CONFFILE.dpkg-remove" ]; then
  110.         echo "Removing obsolete conffile $CONFFILE ..."
  111.         rm -f "$CONFFILE.dpkg-remove"
  112.     fi
  113. }
  114.  
  115. abort_rm_conffile() {
  116.     local CONFFILE="$1"
  117.  
  118.     if [ -e "$CONFFILE.dpkg-remove" ]; then
  119.         echo "Reinstalling $CONFFILE that was moved away"
  120.         mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  121.     fi
  122.     if [ -e "$CONFFILE.dpkg-backup" ]; then
  123.         echo "Reinstalling $CONFFILE that was backupped"
  124.         mv "$CONFFILE.dpkg-backup" "$CONFFILE"
  125.     fi
  126. }
  127.  
  128. ##
  129. ## Functions to rename a conffile during upgrade
  130. ##
  131. mv_conffile() {
  132.     local OLDCONFFILE="$1"
  133.     local NEWCONFFILE="$2"
  134.     local LASTVERSION="$3"
  135.     local PACKAGE="$4"
  136.     if [ "$LASTVERSION" = "--" ]; then
  137.         LASTVERSION=""
  138.         PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE"
  139.     fi
  140.     if [ "$PACKAGE" = "--" -o -z "$PACKAGE" ]; then
  141.         PACKAGE="$DPKG_MAINTSCRIPT_PACKAGE"
  142.     fi
  143.     # Skip remaining parameters up to --
  144.     while [ "$1" != "--" -a $# -gt 0 ]; do shift; done
  145.     [ $# -gt 0 ] || badusage
  146.     shift
  147.  
  148.     [ -n "$PACKAGE" ] || error "couldn't identify the package"
  149.     [ -n "$1" ] || error "maintainer script parameters are missing"
  150.     [ -n "$DPKG_MAINTSCRIPT_NAME" ] || \
  151.         error "environment variable DPKG_MAINTSCRIPT_NAME is required"
  152.  
  153.     debug "Executing $0 mv_conffile in $DPKG_MAINTSCRIPT_NAME "\
  154.           "of $DPKG_MAINTSCRIPT_PACKAGE"
  155.     debug "CONFFILE=$OLDCONFFILE -> $NEWCONFFILE PACKAGE=$PACKAGE "\
  156.           "LASTVERSION=$LASTVERSION ACTION=$1 PARAM=$2"
  157.     case "$DPKG_MAINTSCRIPT_NAME" in
  158.     preinst)
  159.         if [ "$1" = "install" -o "$1" = "upgrade" ] && [ -n "$2" ] &&
  160.            dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  161.             prepare_mv_conffile "$OLDCONFFILE" "$PACKAGE"
  162.         fi
  163.         ;;
  164.     postinst)
  165.         if [ "$1" = "configure" ] && [ -n "$2" ] &&
  166.            dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  167.             finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE"
  168.         fi
  169.         ;;
  170.     postrm)
  171.         if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
  172.            [ -n "$2" ] &&
  173.            dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
  174.             abort_mv_conffile "$OLDCONFFILE"
  175.         fi
  176.         ;;
  177.     *)
  178.         debug "$0 mv_conffile not required in $DPKG_MAINTSCRIPT_NAME"
  179.         ;;
  180.     esac
  181. }
  182.  
  183. prepare_mv_conffile() {
  184.     local CONFFILE="$1"
  185.     local PACKAGE="$2"
  186.  
  187.     [ -e "$CONFFILE" ] || return 0
  188.  
  189.     local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
  190.     local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
  191.         sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
  192.     if [ "$md5sum" = "$old_md5sum" ]; then
  193.         mv -f "$CONFFILE" "$CONFFILE.dpkg-remove"
  194.     fi
  195. }
  196.  
  197. finish_mv_conffile() {
  198.     local OLDCONFFILE="$1"
  199.     local NEWCONFFILE="$2"
  200.  
  201.     rm -f $OLDCONFFILE.dpkg-remove
  202.  
  203.     [ -e "$OLDCONFFILE" ] || return 0
  204.  
  205.     echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
  206.     mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
  207.     mv -f "$OLDCONFFILE" "$NEWCONFFILE"
  208. }
  209.  
  210. abort_mv_conffile() {
  211.     local CONFFILE="$1"
  212.  
  213.     if [ -e "$CONFFILE.dpkg-remove" ]; then
  214.         echo "Reinstalling $CONFFILE that was moved away"
  215.         mv "$CONFFILE.dpkg-remove" "$CONFFILE"
  216.     fi
  217. }
  218.  
  219. # Common functions
  220. debug() {
  221.     if [ -n "$DPKG_DEBUG" ]; then
  222.         echo "DEBUG: $PROGNAME: $1" >&2
  223.     fi
  224. }
  225.  
  226. error() {
  227.     echo "$PROGNAME: error: $1" >&2
  228.     exit 1
  229. }
  230.  
  231. warning() {
  232.     echo "$PROGNAME: warning: $1" >&2
  233. }
  234.  
  235. usage() {
  236.     cat <<END
  237. Syntax: $0 <command> <parameters> -- <maintainer script parameters>
  238.  
  239. Commands and parameters:
  240.  
  241.   supports <command>
  242.     Returns 0 (success) if the given command is supported, 1
  243.     otherwise.
  244.  
  245.   rm_conffile <conffile> [<last-version> [<package>]]
  246.     Remove obsolete conffile.
  247.     Must be called in preinst, postinst and postrm.
  248.  
  249.   mv_conffile <old-conf> <new-conf> [<last-version> [<package>]]
  250.     Rename a conffile.
  251.     Must be called in preinst, postinst and postrm.
  252.  
  253.   help
  254.     Display this usage information.
  255. END
  256. }
  257.  
  258. badusage() {
  259.     usage
  260.     exit 1
  261. }
  262.  
  263. # Main code
  264. set -e
  265.  
  266. PROGNAME=$(basename $0)
  267. version="1.15.8.13"
  268. command="$1"
  269. [ $# -gt 0 ] || badusage
  270. shift
  271.  
  272. case "$command" in
  273. supports)
  274.     case "$1" in
  275.     rm_conffile|mv_conffile)
  276.         code=0
  277.         ;;
  278.     *)
  279.         code=1
  280.         ;;
  281.     esac
  282.     if [ -z "$DPKG_MAINTSCRIPT_NAME" ]; then
  283.         warning "environment variable DPKG_MAINTSCRIPT_NAME missing"
  284.         code=1
  285.     fi
  286.     if [ -z "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  287.         warning "environment variable DPKG_MAINTSCRIPT_PACKAGE missing"
  288.         code=1
  289.     fi
  290.     exit $code
  291.     ;;
  292. rm_conffile)
  293.     rm_conffile "$@"
  294.     ;;
  295. mv_conffile)
  296.     mv_conffile "$@"
  297.     ;;
  298. --help|help|-?|-h)
  299.     usage
  300.     ;;
  301. --version)
  302.     cat <<-END
  303.     Debian $PROGNAME version $version.
  304.  
  305.     Copyright (C) 2010 Rapha├½l Hertzog <hertzog@debian.org>
  306.     Copyright (C) 2008 Joey Hess <joeyh@debian.org>
  307.     Copyright (C) 2007 Guillem Jover <guillem@debian.org>
  308.     Copyright (C) 2005 Scott James Remnant
  309.  
  310.     This is free software; see the GNU General Public License version 2 or
  311.     later for copying conditions. There is NO warranty.
  312.     END
  313.     ;;
  314. *)
  315.     cat >&2 <<-END
  316.     $PROGNAME: error: command $command is unknown
  317.     Hint: upgrading dpkg to a newer version might help.
  318.  
  319.     END
  320.     usage
  321.     exit 1
  322. esac
  323.  
  324. exit 0
  325.